/**
 * 
 */
package org.wicket.jbpm.engine.workflow;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wicket.commons.config.WfConfigurationHelper;
import org.wicket.jbpm.page.WicketJBPMBasePage;

/**
 * @author Emmanuel Nollase - emanux
 * 
 */
public final class WfJbpmFactory {

	private static final Log log = LogFactory.getLog(WfJbpmFactory.class);

	private static final Map<String, String> REPORT_CLASS = WfConfigurationHelper
			.getConfiguration("wf-tasks.task-page(", ")[@name]",")[@wicketclass]");

	public static String getTaskClass(final String taskname) {

		final String clazz = REPORT_CLASS.get(taskname);
		if (StringUtils.isEmpty(clazz)) {
			return null;
		}
		return clazz;
	}
	
	public static WicketJBPMBasePage getTaskPage(final String task, WfTask wfTask) {
	    WicketJBPMBasePage wicketPage = null;
		try{
			final String taskClass = getTaskClass(task);
			final Constructor<?> clazz = Class.forName(taskClass).getConstructor(WfTask.class);
			wicketPage = (WicketJBPMBasePage) clazz.newInstance(wfTask);
		
		}catch(NullPointerException e) {
			log.error("NullPointerException Error occured", e);
		} catch (ClassNotFoundException e) {
			log.error("ClassNotFoundException Error occured",e);
		} catch (SecurityException e) {
			log.error("SecurityException Error occured", e);
		} catch (NoSuchMethodException e) {
			log.error("NoSuchMethodException Error occured", e);
		} catch (IllegalArgumentException e) {
			log.error("IllegalArgumentException Error occured", e);
		} catch (InstantiationException e) {
			log.error("InstantiationException Error occured", e);
		} catch (IllegalAccessException e) {
			log.error("IllegalAccessException Error occured", e);
		} catch (InvocationTargetException e) {
			log.error("InvocationTargetException Error occured", e);
		}
		
		return wicketPage;
	}
}
